#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

#define     BUFLEN  1024

#define erraise(string) do{\
    perror(string);\
    exit(-1);\
}while(0)

int main()
{
    int sockfd, n;
    char buffer[BUFLEN];
    struct sockaddr_in servaddr;
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(8080);
    
    inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
    if ((connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) < 0)
       erraise("connect to server failed:");
    printf(">>> ");
    printf("connected....\n");
    memset(buffer, 0, sizeof(buffer));
    gets(buffer);
    write(sockfd, buffer, sizeof(buffer));
    memset(buffer, 0, sizeof(buffer));
    read(sockfd, buffer, sizeof(buffer));
    printf(">>> %s\n", buffer);
    return 0;
}

